package com.jg.visitor.kafka.producer;

import com.jg.visitor.common.bean.WechatInfoBean;
import com.jg.visitor.common.utils.JsonService;
import com.jg.visitor.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class MsgSendService {

    @Resource
    private KafkaTemplate<String,String> kafkaTemplate;
    @Resource
    private JsonService jsonService;

    private static final String WECHAT_TOPIC = "kafka-topic-wechat";

    public void sendMsg(String topic,String msg){
        if(StringUtils.isEmpty(topic)){
            throw new RuntimeException("kafka主题不能为空！");
        }

        kafkaTemplate.send(topic, msg);
    }

    public void sendWechatMsg(WechatInfoBean wechatInfoBean){
        String json = jsonService.objectToJson(wechatInfoBean);

        sendMsg(WECHAT_TOPIC,json);
    }
}
